﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Text;

namespace PNProject
{
    public static class PNLibraries
    {
        public static string PNChuyenKhongDau(string ip_str_change)
        {
            if (!string.IsNullOrEmpty(ip_str_change))
            {
                Regex v_reg_regex = new Regex("\\p{IsCombiningDiacriticalMarks}+");
                string v_str_FormD = ip_str_change.Normalize(NormalizationForm.FormD);
                var st = v_reg_regex.Replace(v_str_FormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
                return PNUrlFormatConverter(st);
            }
            else
                return "";
        }

        public static string PNVietNamNumberFormat(long number, string split_char)
        {
            if (number != null)
            {
                string num = number.ToString();
                int leng = num.Length;
                string result = "";
                for (int i = 0; i < leng; i++)
                {
                    string c = num.Substring(leng - i - 1, 1);
                    if (i % 3 == 0 && i > 0)
                    {
                        result = c + split_char + result;
                    }
                    else
                    {
                        result = c + result;
                    }
                }
                return result;
            }
            else
                return "";

        }

        public static string PNUrlFormatConverter(string input)
        {
            input = input.ToLower();
            var chars = input.ToCharArray();
            string result = "";
            bool flag = true;
            for (int i = 0; i < chars.Length; i++)
            {
                if ((chars[i] >= 97 && chars[i] <= 122) || (chars[i] >= 48 && chars[i] <= 57))
                {
                    result = result + chars[i].ToString();
                    flag = true;
                }
                else
                {
                    if (flag)
                    {
                        result = result + "-";
                        flag = false;
                    }
                }
            }
            if (!string.IsNullOrEmpty(result))
            {
                var c = result.Substring(0, 1);
                while (c == "-")
                {
                    if (result.Length > 1)
                    {
                        result = result.Substring(1, result.Length - 1);
                        c = result.Substring(0, 1);
                    }
                    else
                    {
                        result = "";
                        c = "";
                    }
                }
            }

            if (!string.IsNullOrEmpty(result))
            {
                var c = result.Substring(result.Length - 1, 1);
                while (c == "-")
                {
                    if (result.Length > 1)
                    {
                        result = result.Substring(0, result.Length - 1);
                        c = result.Substring(result.Length - 1, 1);
                    }
                    else
                    {
                        result = "";
                        c = "";
                    }
                }
            }

            return result;
        }

        public static long PNConvertToTimestamp(DateTime value)
        {
            long epoch = (value.Ticks - 621355968000000000) / 10000000;
            return epoch;
        }

        public static DateTime PNTimeStampToDateTime(double unixTimeStamp)
        {
            // Unix timestamp is seconds past epoch
            System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            dtDateTime = dtDateTime.AddSeconds(unixTimeStamp);
            return dtDateTime;
        }

        public static string PNChuanHoaTen(string name)
        {
            return "";
        }

        public static List<int> GetShowedPage(int totalCount, int activePage, int itemPerPage, int limitPage)
        {
            List<int> result = new List<int>();
            int countPage;
            if (totalCount % itemPerPage != 0)
                countPage = totalCount / itemPerPage + 1;
            else
                countPage = totalCount / itemPerPage;
            if (activePage >= countPage)
            {
                if (countPage <= limitPage)
                {
                    for (int i = 0; i < countPage; i++)
                    {
                        result.Add(i + 1);
                    }
                }
                else
                {
                    for (int i = 4; i >= 0; i--)
                    {
                        result.Add(countPage - i);
                    }
                }
            }
            else
            {
                if (countPage >= limitPage)
                {
                    if (activePage <= 3)
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            result.Add(i + 1);
                        }
                    }
                    else
                    {
                        result.Add(activePage - 2);
                        result.Add(activePage - 1);
                        result.Add(activePage);
                        if (activePage + 1 <= countPage)
                            result.Add(activePage + 1);
                        if (activePage + 2 <= countPage)
                            result.Add(activePage + 2);
                    }
                }
                else
                {
                    for (int i = 0; i < countPage; i++)
                    {
                        result.Add(i + 1);
                    }
                }
            }
            return result;

        }

        public static string PNTimeFormat(DateTime time, bool type, bool lang)
        {
            string thu = time.DayOfWeek.ToString();
            string vnthu = "";
            switch (thu)
            {
                case "Monday":
                    {
                        vnthu = lang ? "Thứ hai" : "Monday";
                        break;
                    }
                case "Tuesday":
                    {
                        vnthu = lang ? "Thứ ba" : "Tuesday";
                        break;
                    }
                case "Wednesday":
                    {
                        vnthu = lang ? "Thứ tư" : "Wednesday";
                        break;
                    }
                case "Thursday":
                    {
                        vnthu = lang ? "Thứ năm" : "Thursday";
                        break;
                    }
                case "Friday":
                    {
                        vnthu = lang ? "Thứ sáu" : "Friday";
                        break;
                    }
                case "Saturday":
                    {
                        vnthu = lang ? "Thứ bảy" : "Saturday";
                        break;
                    }
                case "Sunday":
                    {
                        vnthu = lang ? "Chủ nhật" : "Sunday";
                        break;
                    }

            }
            string ngay = time.Day < 10 ? "0" + time.Day.ToString() : time.Day.ToString();
            string thang = time.Month < 10 ? "0" + time.Month.ToString() : time.Month.ToString();
            string gio = time.Hour < 10 ? "0" + time.Hour.ToString() : time.Hour.ToString();
            string phut = time.Minute < 10 ? "0" + time.Minute.ToString() : time.Minute.ToString();
            string gp = " " + gio + ":" + phut;
            if (type)
                return vnthu + ", ngày " + ngay + " tháng " + thang + " năm " + time.Year.ToString() + gp;
            else
                return vnthu + ", " + ngay + "-" + thang + "-" + time.Year.ToString() + gp;
        }

        public static string RepaceAuthenLink(string source, string linkMask, string imgMask)
        {
            int index = source.IndexOf("<a href=");
            while (index > 0)
            {
                if (index >= 3)
                {
                    if (source.Substring(index - 3, 3) == "[p]")
                    {
                        string founded = "";
                        int end = source.IndexOf("</a>", index);
                        if (end < 0)
                        {
                            break;
                        }
                        else
                        {
                            founded = source.Substring(index - 3, end - index + 7);
                            source = source.Replace(founded, linkMask);
                        }
                    }
                   
                }
                index = source.IndexOf("<a href=", index + 2);
            }

            index = source.IndexOf("<img");
            while (index > 0)
            {
                if (index >= 3)
                {
                    if (source.Substring(index - 3, 3) == "[p]")
                    {
                        string founded = "";
                        int end = source.IndexOf("/>", index);
                        if (end < 0)
                        {
                            break;
                        }
                        else
                        {
                            founded = source.Substring(index - 3, end - index + 5);
                            source = source.Replace(founded, imgMask);
                        }
                    }

                }
                index = source.IndexOf("<img", index + 2);
            }

            return source;
        }

        public static bool isValidEmail(string inputEmail)
        {
            string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                  @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                  @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            Regex re = new Regex(strRegex);
            if (re.IsMatch(inputEmail))
                return (true);
            else
                return (false);
        }

        public static int PageVerify(string page)
        {
            int p = 1;
            try
            {
                p = int.Parse(page);
            }
            catch (Exception e) { }
            return p;
        }

        public static List<int> RandomInList(int max, int count)
        {
            List<int> result = new List<int>();
            List<int> maxArr = new List<int>();
            for (int i = 0; i < max; i++)
            {
                maxArr.Add(i);
            }
            if (count >= max)
            {
                return maxArr;
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    Random r = new Random();
                    int rand = r.Next(maxArr.Count - 1);
                    result.Add(maxArr.ElementAt(rand));
                    maxArr.RemoveAt(rand);
                }
                return result;
            }
        }



    }
}